<template>
	<view class="signInCoupon" >
		<image @click="wxRegister()" src="https://yuyitang.oss-cn-shenzhen.aliyuncs.com/yytfile/upload/2022/09/17/bfbda1ee-2344-43f8-807a-6276d89e7252.jpg" style="width: 100vw; height: 100vh;"></image>
		<!-- <view v-if="!userIsDraw" class="right-card-box" @click="wxRegister()">
			<view class="sharing-btn" >领取</view>
		</view> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				userIsDraw: false
			}
		},
		onLoad(e) {
			this.getUserCoupon()
		},
		computed: {
			...mapState(['applicationConfig','session_key','openid','token'])
		},
		methods: {
			// 跳转到首页
			getHomePage(){
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/sys/home/index'
					})
				}, 2000)
			},
			// 获取该用户是否已经领取了优惠券
			getUserCoupon(){
				const that = this
				uni.login({
				  success: (res) => {
				    // console.log(res);
				    this.$api.request.getOpenid({
				      code: res.code
				    }, res => {
				      // console.log(res);
				      if (res.code == 200) {
				        let data = JSON.parse(res.data)
						uni.setStorageSync("openId", data.openid)
				        this.$store.commit("setOpenId", data)
				        this.$api.request.getMyIsCouponList({
				        	openId: data.openid
				        }, res => {
				        	console.log(res);
				        	if(res.code == 200 && res.data == true){
				        		this.userIsDraw = true
				        		uni.showModal({
				        			title: '温馨提示',
				        			content: '不可重复领取',
				        			showCancel: false,
				        			success: (res) => {
										that.getHomePage()
				        			}
				        		})
				        	}
				        })
				      }
				    })
				  }
				})			
			},
			// 授权注册
			wxRegister(){
				const that = this
				if(!this.userIsDraw){
					uni.getUserProfile({
						desc: "获取你的昵称、头像、地区及性别",
						success: (res) => {
							// console.log(res);
							this.$api.request.addOpenid({
								encryptedData: res.encryptedData,
								iv: res.iv,
								sessionKey: this.session_key,
								wechatId: this.openid,
								sourceType:"APP"
							},res=>{
								// this.$api.msg(res.data)
								if(res.code==200){
									this.$api.request.getWxUser({
										wechatId: this.openid
									}, res => {
										if (res.code == 200&&res.data!=404) {
											this.$api.msg("领取成功！")
											uni.setStorageSync('userInfo', res.data)
											this.$store.commit("setToken", uni.getStorageSync(
												'userToken'))
											this.$store.commit("setToken", res.data.tokenValue)
											// 已授权
											this.author = true
											this.$store.commit("login", res.data);
											that.getHomePage()
										}else{
											setTimeout(function() {
												uni.showToast({
													title: res.message,
													icon: 'none'
												})
											},100)
										}
									})
								}else if(res.code == 205){
									setTimeout(function() {
										uni.showToast({
											title: res.message,
											icon: 'none'
										})
										setTimeout(() => {
											uni.hideLoading()
										})
									},100)
								}
							})
							
						}
					})
				}else{
					this.$api.msg("不可重复领取")
					setTimeout(() => {
						uni.switchTab({
							url: 'pages/sys/home/index'
						})
					}, 2000)
				}
			},
		}
	}
</script>

<style lang="scss">
	.signInCoupon{
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	
	.right-card-box {
		width: 36%;
		height: 200rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 42rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #000000;
		line-height: 30px;
		margin-top: 60rpx;
		margin-right: 20rpx;
	}
	
	.sharing-btn {
		color: lightgreen;
		background: none;
		border: none;
	}
</style>